// pages/preference/preference.js
/**
 * 忌口设置页面
 * - 肉类、蛋奶豆制品、蔬菜分类选择
 * - 多选忌口食材
 * - 数据持久化到本地：key = 'dietaryRestrictions'
 */
Page({
  data: {
    // 肉类
    meats: [
      { name: '猪', selected: false },
      { name: '牛', selected: false },
      { name: '羊', selected: false },
      { name: '鸡', selected: false },
      { name: '鸭', selected: false },
      { name: '鹅', selected: false },
      { name: '牛蛙', selected: false },
      { name: '驴', selected: false },
      { name: '兔', selected: false },
      { name: '鸽', selected: false },
      { name: '鹌鹑', selected: false },
      { name: '虾', selected: false },
      { name: '蟹', selected: false },
      { name: '鱼', selected: false },
      { name: '其他海鲜', selected: false }
    ],
    
    // 蛋奶豆制品
    eggDairySoy: [
      { name: '蛋', selected: false },
      { name: '牛奶', selected: false },
      { name: '羊奶', selected: false },
      { name: '豆制品', selected: false }
    ],
    
    // 蔬菜
    vegetables: [
      { name: '苦瓜', selected: false },
      { name: '青椒', selected: false },
      { name: '胡萝卜', selected: false },
      { name: '其他萝卜', selected: false },
      { name: '茼蒿', selected: false },
      { name: '折耳根', selected: false },
      { name: '茴香', selected: false },
      { name: '韭菜', selected: false },
      { name: '洋葱', selected: false },
      { name: '香菜', selected: false },
      { name: '香椿', selected: false },
      { name: '娃娃菜', selected: false },
      { name: '生菜', selected: false },
      { name: '菠菜', selected: false },
      { name: '白菜', selected: false },
      { name: '小白菜', selected: false },
      { name: '芹菜', selected: false },
      { name: '西兰花', selected: false },
      { name: '菜花/花菜', selected: false },
      { name: '空心菜', selected: false },
      { name: '卷心菜', selected: false },
      { name: '紫甘蓝', selected: false },
      { name: '油麦菜', selected: false },
      { name: '苦菊', selected: false },
      { name: '蒜苗', selected: false },
      { name: '芥菜', selected: false },
      { name: '冰草', selected: false },
      { name: '西洋菜', selected: false },
      { name: '蕨菜', selected: false },
      { name: '黄花菜', selected: false },
      { name: '荠菜', selected: false },
      { name: '地瓜叶', selected: false },
      { name: '丝瓜苗', selected: false },
      { name: '蒜苔', selected: false },
      { name: '菜心', selected: false },
      { name: '羽衣甘蓝', selected: false },
      { name: '苋菜', selected: false },
      { name: '马兰头', selected: false },
      { name: '莴笋叶', selected: false },
      { name: '芥兰', selected: false },
      { name: '豆苗', selected: false },
      { name: '紫苏叶', selected: false },
      { name: '豌豆尖', selected: false },
      { name: '贡菜', selected: false },
      { name: '芝麻菜', selected: false },
      { name: '孢子甘蓝', selected: false },
      { name: '南瓜', selected: false },
      { name: '番茄', selected: false },
      { name: '冬瓜', selected: false },
      { name: '黄瓜', selected: false },
      { name: '茄子', selected: false },
      { name: '丝瓜', selected: false },
      { name: '西葫芦', selected: false },
      { name: '瓠瓜', selected: false },
      { name: '甜椒', selected: false },
      { name: '辣椒', selected: false },
      { name: '秋葵', selected: false },
      { name: '芋头', selected: false },
      { name: '藕', selected: false },
      { name: '土豆', selected: false },
      { name: '红薯', selected: false },
      { name: '玉米', selected: false },
      { name: '山药', selected: false },
      { name: '茭白', selected: false },
      { name: '芦笋', selected: false },
      { name: '莴笋', selected: false },
      { name: '玉米笋', selected: false },
      { name: '竹笋', selected: false },
      { name: '儿菜', selected: false },
      { name: '紫薯', selected: false },
      { name: '荸荠', selected: false },
      { name: '甜菜根', selected: false },
      { name: '木薯', selected: false },
      { name: '栗子', selected: false },
      { name: '慈菇', selected: false },
      { name: '豆芽', selected: false },
      { name: '豆角类', selected: false },
      { name: '花生', selected: false },
      { name: '艾叶', selected: false },
      { name: '蘑菇', selected: false },
      { name: '木耳', selected: false },
      { name: '地衣', selected: false }
    ]
  },

  onLoad() {
    // 读取本地忌口设置
    const restrictions = wx.getStorageSync('dietaryRestrictions')
    if (restrictions && typeof restrictions === 'object') {
      this.setData({
        meats: restrictions.meats || this.data.meats,
        eggDairySoy: restrictions.eggDairySoy || this.data.eggDairySoy,
        vegetables: restrictions.vegetables || this.data.vegetables
      })
    }
  },

  // 切换肉类选择
  toggleMeat(e) {
    const index = e.currentTarget.dataset.index
    const meats = this.data.meats
    meats[index].selected = !meats[index].selected
    this.setData({ meats })
  },

  // 切换蛋奶豆制品选择
  toggleEggDairySoy(e) {
    const index = e.currentTarget.dataset.index
    const eggDairySoy = this.data.eggDairySoy
    eggDairySoy[index].selected = !eggDairySoy[index].selected
    this.setData({ eggDairySoy })
  },

  // 切换蔬菜选择
  toggleVegetable(e) {
    const index = e.currentTarget.dataset.index
    const vegetables = this.data.vegetables
    vegetables[index].selected = !vegetables[index].selected
    this.setData({ vegetables })
  },

  // 保存设置
  onSave() {
    const restrictions = {
      meats: this.data.meats,
      eggDairySoy: this.data.eggDairySoy,
      vegetables: this.data.vegetables
    }
    
    // 保存到本地存储
    wx.setStorageSync('dietaryRestrictions', restrictions)
    
    // 同时更新用户数据管理器中的过敏信息
    const user = wx.getStorageSync('user') || {}
    const selectedItems = []
    
    // 收集所有选中的忌口食材
    this.data.meats.forEach(item => {
      if (item.selected) selectedItems.push(item.name)
    })
    this.data.eggDairySoy.forEach(item => {
      if (item.selected) selectedItems.push(item.name)
    })
    this.data.vegetables.forEach(item => {
      if (item.selected) selectedItems.push(item.name)
    })
    
    user.allergy = selectedItems.join('、')
    wx.setStorageSync('user', user)
    
    wx.showToast({ 
      title: '忌口设置已保存', 
      icon: 'success' 
    })
    
    setTimeout(() => {
      wx.navigateBack({ delta: 1 })
    }, 1500)
  },

  // 全选/取消全选
  toggleAll(category) {
    let items = []
    let updateKey = ''
    
    switch (category) {
      case 'meats':
        items = this.data.meats
        updateKey = 'meats'
        break
      case 'eggDairySoy':
        items = this.data.eggDairySoy
        updateKey = 'eggDairySoy'
        break
      case 'vegetables':
        items = this.data.vegetables
        updateKey = 'vegetables'
        break
    }
    
    const allSelected = items.every(item => item.selected)
    items.forEach(item => {
      item.selected = !allSelected
    })
    
    this.setData({
      [updateKey]: items
    })
  },

  // 清空所有选择
  clearAll() {
    wx.showModal({
      title: '确认清空',
      content: '确定要清空所有忌口设置吗？',
      success: (res) => {
        if (res.confirm) {
          const meats = this.data.meats.map(item => ({ ...item, selected: false }))
          const eggDairySoy = this.data.eggDairySoy.map(item => ({ ...item, selected: false }))
          const vegetables = this.data.vegetables.map(item => ({ ...item, selected: false }))
          
          this.setData({
            meats,
            eggDairySoy,
            vegetables
          })
          
          wx.showToast({
            title: '已清空',
            icon: 'success'
          })
        }
      }
    })
  }
})


